
var current_cid = '';
var current_pxicd = '';
var global_featured = new Array()

var hide = function() {
	if (current_cid != '') {
		var oldbox = getObject('customize_'+current_cid);
		sourcebox.innerHTML = oldbox.innerHTML;
		oldbox.innerHTML = '';
		current_cid = '';
		current_pxcid = '';
	}
}

var customize = function(cid, pxcid) {
	hide();
	var customizebox = getObject('customize_'+cid);
	var sourcebox = getObject('sourcebox'); 
	customizebox.innerHTML = sourcebox.innerHTML;
	sourcebox.innerHTML = '';
	current_cid = cid;
	current_pxcid = pxcid;
	return false;
}

var save = function() {
	hide();
}

var loadCurrentTicker = function(cid) {
    var box = getObject('ticker_'+cid);
    box.innerHTML = box.innerHTML;
}

var currentTab = ''
var selectTab = function(tab_name) {
	if (currentTab != '') {
		var tab = 'tab_'+currentTab;
		getObject(tab).className = ''
		var block = 'block_'+currentTab;
		getObject(block).className = 'hidden_block'
	}
	currentTab = tab_name;
	var tab = 'tab_'+currentTab;
	getObject(tab).className = 'selected'
	var block = 'block_'+currentTab;
	getObject(block).className = ''
	return false;
}

var updateCurrentTicker = function(params, pxcid, cid) {
	if (pxcid == '') {
		pxcid = current_pxcid;
	}
	if (cid == '') {
		cid = current_cid;
	}
	if (pxcid != '') {
		params['pxcid'] = pxcid;
		params['xaction'] = "update_ticker";
		asyncAction(
			"channelajax",
			params,
			function (theResponse) {
				loadCurrentTicker(cid)
			}
		);
	}
	return false;
}

var updateFBML = function(pxfbuid, session, pxuid, cid, checked, xaction, id) {
	ShowUpdate(id, 0, 0, 'Updating...')
	
	if (checked) {
		global_featured.push(cid);
	} else {
		var new_array = []
		for (i=0;i<global_featured.length;i++ ) {
			if (global_featured[i] != cid) {
				new_array.push(global_featured[i]);
			}
		}
		global_featured = new_array
	}
	
	var fs = global_featured.join(',')
	
	params = {
		'pxuid'    : pxuid,
		'pxfbuid'  : pxfbuid,
		'session'  : session,
		'xaction'  : xaction,
		'featured' : fs,
		'checked'  : checked,
		'cid'      : cid
	}
	asyncAction(
			"fbajax",
			params,
			function (theResponse) {
				if (getObject(id)) {
					if (theResponse.substring(0,4) == "PASS") {
						ShowUpdate(id, 0, 0, 'Profile Updated')
					} else {
						ShowUpdate(id, 0, 0, 'Failed Update<br/>Please Try Again')
					}
				}
			}
		);
}

var clearField = function(id) {
	var preview = getObject('preview_'+id);
	preview.style.display = "none";
	
	var input_field = getObject(id);
	input_field.style.display = "block";
	input_field.focus();
}

var deleteChannel = function(cid, container, pxfbuid, pxuid, session, url, product){
	id = 'update_' + cid
	if (getObject(id).checked) {
		alert('You cannot remove a ' + product + ' that is featured on your profile.\n Please uncheck "Show on Profile" before deleting.')
	} else {
		message = "Are you sure you want to delete this " + product + "?\n 1. It will be removed from this page and from Slide.com\n 2. It will NOT affect your albums on Facebook.";
		if (confirm(message)){
			asyncAction('channelajax', {'xaction' : 'removechannel',
				'cid' : cid},
				function (theResponse) {
					if (theResponse.substring(0,4) == "PASS") {
						window.parent.location.replace(url)
					}
				});
		}
	}
	
	return false;
}	

var deleteItem = function(iid, container, pxfbuid, pxuid, session, url) {
	id = 'update_' + iid
	if (getObject(id).checked) {
		alert('You cannot delete a featured item, please uncheck it before deleting.')
	} else {
		message = 'Are you sure you want to delete this item?';
		if (confirm(message)) {
			asyncAction('channelajax', {'xaction' : 'deleteitem',
				'ciid'    : iid},
				function(theResponse) {
					if (theResponse.substring(0,4) == "PASS") {
						window.parent.location.replace(url)
					}
				});
		}	
	}
	return false;
}
	
var sendInvite = function(id, fbuid, session, app_name, to_uids){
	to_uids = to_uids || [];
	
	asyncAction('fbajax',
		{'xaction' : 'send_invite',
			'fb_uid' : fbuid,
			'fb_session_key' : session,
			'app_name' : app_name,
			'to_uids' : to_uids },
		function (theResponse) {
			if (theResponse.substring(0,4) == "PASS") {
				parts = theResponse.split('url:');
				url = parts[1];
				if (url){
					url = url + '&next=?next=' + app_name;
					parent.location.href=url;
				}	
				ShowUpdate(id, 0, 50, 'Invite sent.')
				
			}	
		});	
	return false;
}

var setTopFriends = function(id, fbuid, session, old_uids, totaluids, top_uids) {
	top_uids = top_uids || [];
	old_uids = old_uids || [];
	totaluids = totaluids || 0;
	ShowUpdate(id, -40, 50, 'Updating...')
	asyncAction('fb_top8_ajax',
		{ 'xaction' : 'update_top_friends',
			'fb_uid' : fbuid,
			'fb_session_key' : session,
			'old_uids' : old_uids,
			'totaluids' : totaluids,
			'top_uids' : top_uids
			},
		function (theResponse) {
			if (theResponse.substring(0,4) == "PASS") {
				parts = theResponse.split('url:');
				url = parts[1];
				if (url){
					parent.location.href=url;
				}	
				ShowUpdate(id, -40, 50, 'Top Friends Updated.')
			}
		}	
	);

	return false;
}	

var selectAll = function(checked) {
	var box = getObject('check_0');
	var count = 0;
	while(box) {
		box.checked = checked
		count++;
		box = getObject('check_' + count)
	}
}

var profileUrl = function(fb_uid) { 
	return "http://www.facebook.com/profile.php?id=" + fb_uid;
}	

var messageUrl = function(fb_uid) {
	return "http://www.facebook.com/message.php?id=" + fb_uid;
}

var pokeUrl = function(fb_uid) {
	return "http://www.facebook.com/poke.php?id=" + fb_uid;
}	

var friendsUrl = function(fb_uid) {
	return "http://www.facebook.com/friends.php?id=" + fb_uid;
}	

var firstName = function(fullname) {
	return String(fullname).split(' ', 1);
}	

